package com.qianfeng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qianfeng.common.Constants;
import com.qianfeng.entity.dto.Goods;
import com.qianfeng.entity.dto.GoodsSku;
import com.qianfeng.entity.vo.MyPage;
import com.qianfeng.mapper.GoodsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.qianfeng.utils.MyUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class GoodsService extends ServiceImpl<GoodsMapper, Goods> {

    @Autowired
    private GoodsSkuService goodsSkuService;

    @Transactional(readOnly = true)
    public MyPage<Goods> getByMerchantId(Long merchantId , Long pageNum) {
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Goods::getMerchantId, merchantId);

        IPage<Goods> page =this.page(new Page<>(pageNum , Constants.PAGE_SIZE), wrapper);

//        IPage<Goods> page = this.page(new Page<>(pageNum, Constants.PAGE_SIZE), wrapper);

        MyPage<Goods> myPage = MyUtils.getMyPage(page);
        return myPage;
    }

    @Transactional(readOnly = true)
    public MyPage<Goods> getByGoodTypeId(Long goodsTypeId , Long pageNum) {
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        if(goodsTypeId != 0L){
            wrapper.lambda().eq(Goods::getTypeId , goodsTypeId);
        }

//        MyPage<Goods> myPage = new MyPage<>();
        IPage<Goods> page = this.page(new Page<>(pageNum, Constants.PAGE_SIZE), wrapper);

        MyPage<Goods> myPage = MyUtils.getMyPage(page);

//        myPage.setObjList(page.getRecords());
//        Long pages = page.getPages();
//        List<Integer> pageList = new ArrayList<>(pages.intValue());
//        for (int i = 1; i <= pages; i++) {
//            pageList.add(i);
//        }
//        myPage.setPageList(pageList);
        return myPage;
    }

    @Transactional
    public void up(Long goodsId) {
        UpdateWrapper<Goods> wrapper = new UpdateWrapper<Goods>();
        wrapper.lambda().eq(Goods::getId, goodsId).set(Goods::getState , 1L);
        this.update(wrapper);
    }

    @Transactional
    public void down(Long goodsId) {
        UpdateWrapper<Goods> wrapper = new UpdateWrapper<Goods>();
        wrapper.lambda().eq(Goods::getId, goodsId).set(Goods::getState , 2L);
        this.update(wrapper);
    }

    @Transactional
    public void saveGoods(Goods goods , Long merchantId) {
        if (goods.getId() != null){
            goodsSkuService.removeByGoodsId(goods.getId());
        }

        goods.setMerchantId(merchantId);
        goods.setState(0L);
        this.saveOrUpdate(goods);

        String[] titles = goods.getSkuTitle().split("\\|");
        String[] costs = goods.getSkuCost().split("\\|");
        String[] prices = goods.getSkuPrice().split("\\|");
        String[] pmoneys = goods.getSkuPmoney().split("\\|");

        List<GoodsSku> goodsSkus = new ArrayList<>(titles.length);
        int orderNum = 1;

        for (int i = 0; i < titles.length; i++) {
            if (!"|".equals(titles[i])){
                GoodsSku goodsSku = new GoodsSku();
                goodsSku.setGoodsId(goods.getId());
                goodsSku.setTitle(titles[i]);
                goodsSku.setCost(costs[i]);
                goodsSku.setPrice(prices[i]);
                goodsSku.setPmoney(pmoneys[i]);
                goodsSku.setOrderNum(new Long(orderNum));
                goodsSkus.add(goodsSku);
                orderNum++;
            }
        }
        goodsSkuService.saveBatch(goodsSkus , goodsSkus.size());
    }


    @Transactional
    public MyPage<Goods> getUppedByMerchantId(Long merchantId , Long pageNum) {
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Goods::getMerchantId , merchantId).eq(Goods::getState , 1L);
        IPage<Goods> page = this.page(new Page<>(pageNum, Constants.PAGE_SIZE), wrapper);
        MyPage<Goods> myPage = MyUtils.getMyPage(page);
        return myPage;
    }


    @Transactional
    public void removeGoodsById(Long goodsId) {
        this.removeById(goodsId);
        goodsSkuService.removeByGoodsId(goodsId);
    }
}
